package com.zyh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyh.entity.User;
import com.zyh.service.IUserService;
import com.zyh.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-05-04
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;

    @GetMapping("/login")
    public Map<String,String> login(String name, String pwd){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("u_name",name).eq("u_password",pwd);
        User user = userService.getOne(wrapper);
        Map<String,String> map=new HashMap<>();
        if(user==null){
            map.put("msg","登录失败");
        }else{
            map.put("msg","登录成功");
            Map<String,String> jwtMap=new HashMap<>();
            jwtMap.put("name",name);
            jwtMap.put("userId",user.getUId()+"");
            String token = JwtUtils.getToken(jwtMap);
            map.put("token",token);
        }
        return map;
    }

    @GetMapping("/{id}")
    public User selectById(@PathVariable("id") Integer id){
        return userService.getById(id);
    }
}

